package com.along.game.action.front;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import com.along.framework.web.BaseAction;
import com.along.game.dao.InitGameDAO;
import com.along.game.dao.PicDAO;
import com.along.game.dao.RoomDAO;
import com.along.game.dao.UserInfoDAO;
import com.along.game.model.Bigpic;
import com.along.game.model.Gamescene;
import com.along.game.model.GameTable;
import com.along.game.model.InitDeskinfo;
import com.along.game.model.OnlineUser;
import com.along.game.model.SmallStateinfo;
import com.along.game.model.Smallpic;
import com.along.game.model.SysMenu;
import com.along.game.model.UserInfo;

public class InitGameAction extends BaseAction {

	@Autowired
	private InitGameDAO intGameDAO;
	@Autowired
	private RoomDAO roomDAO;
	@Autowired
	private UserInfoDAO userInfoDAO;
	@Autowired
	private PicDAO picDAO;

	public String enterGame() throws IOException {
		String usn = getParameter("usn");// 用户名
		String pwd = getParameter("psw");// 密码
		UserInfo userInfo = new UserInfo();

		userInfo = userInfoDAO.getUserInfo(usn, pwd);

		if (userInfo != null && !"".equals(userInfo.getName())) {
			this.getSession().setAttribute("userinfo", userInfo);// 保存到session中
			OnlineUser onlineUser = new OnlineUser();
			onlineUser.setUsn(userInfo.getUsn());
			onlineUser.setUserName(userInfo.getName());
			onlineUser.setPoints(userInfo.getUserLevel());// 积分
			onlineUser.setUserGradename(userInfo.getUserLevel() + "");// 等级
			onlineUser.setUserSex(userInfo.getSex());// 性别
			onlineUser.setUserState("0");// 用户状态
			onlineUser.setCombocount(0);// 用户状态
			onlineUser.setHits(0);// 用户状态
			userInfoDAO.saveOnlineUser(onlineUser);

			getResponse().getWriter().write("true");// 登录成功
		} else
			getResponse().getWriter().write("false");// 失败
		return null;
	}

	/**
	 * 用户点中桌子进入游戏
	 * 
	 * @return
	 * @throws IOException
	 */
	public String readyGame() throws IOException {
		UserInfo userInfo = new UserInfo();
		userInfo = (UserInfo) this.getSession().getAttribute("userinfo");

		// intGameDAO.SaveRoomInfoByLocalId("1","1","sdsff");//保存用户的位置
		String roomid = getParameter("roomid");// 传入房间id
		String desk_id = getParameter("desk_id");// 传入桌子id
		String strlocal = getParameter("location");// 位置

		// List list = roomDAO.getPlayerByDeskId(desk_id);

		// 第一个人进入房子的时候加载信息
		// 产生五张图片
		Set<Integer> set = new java.util.HashSet<Integer>();
		int ran;
		while (set.size() < 5) {
			ran = (int) Math.round(Math.random() * 100) + 1;//产生5张大图片，大图片为1-100
			set.add(ran);
		}
		Object[] obj = set.toArray();
		String strBigPics = "";// 大图片

		String strmallPis = "";
		for (int i = 0; i < obj.length; i++) {
			strBigPics += obj[i] + ",";

			Set<Integer> set2 = new java.util.HashSet<Integer>();
			int ran2;
			while (set2.size() < 5) {
				ran = (int) Math.round(Math.random() * 20) + 1;//产生五张小图片，小图片，为1-20
				set2.add(ran);
			}
			Object[] obj2 = set2.toArray();
			String strmallPis3 = "";
			for (int ii = 0; ii < obj2.length; ii++) {
				strmallPis3 += obj2[ii] + ",";
			}
			strmallPis = strmallPis
					+ strmallPis3.substring(0, strmallPis3.length() - 1) + "|";

		}

		strmallPis = strmallPis.substring(0, strmallPis.length() - 1);
		strBigPics = strBigPics.substring(0, strBigPics.length() - 1);
		InitDeskinfo initDesk = new InitDeskinfo();
		initDesk.setDeskId(Integer.parseInt(desk_id));
		initDesk.setPicidOrder(strBigPics);
		initDesk.setSmallpicOrder(strmallPis);
		picDAO.saveInitDesk(initDesk);// 保存大小图片到数据库

		OnlineUser onlineUser = new OnlineUser();
		onlineUser = roomDAO.getOnlineInfoByUsn(userInfo.getUsn());

		onlineUser.setFieldId(Integer.parseInt(roomid));// 房间id
		onlineUser.setDeskId(Integer.parseInt(desk_id));// 游戏桌
		onlineUser.setDeskLocation(strlocal);// 所加入的位置,为数字
		onlineUser.setHits(0);// 点击数
		onlineUser.setUserState("0");// 用户状态
		userInfoDAO.saveOnlineUser(onlineUser);
		getResponse().setCharacterEncoding("utf-8");
		getResponse().getWriter().write("1");
		return null;

	}

	/**
	 * 进入房间时，调用此方法
	 * 
	 * @return
	 */
	public String initGame() {
		// InitDeskinfo initDesk = new InitDeskinfo();
		// initDesk.setDeskId(9);
		// initDesk.setId(1);
		// initDesk.setPicidOrder("1,2,3,4,5");
		// intGameDAO.InserInitBigPic(initDesk);

		SmallStateinfo initDesk = new SmallStateinfo();
		initDesk.setDeskId(9);
		initDesk.setBigpicId(2);
		initDesk.setSmallpicIds("6,7,8,9,10");
		intGameDAO.InserInitSmallPic(initDesk);

		return "";
	}

}
